package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CarConfigModel;
import com.jzg.jcptadmin.viewinterface.CarConfigSelectInterface;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 车系选择---参数选择
 * Created by zealjiang on 2016/11/2 20:14.
 * Email: zealjiang@126.com
 */

public class CarConfigSelectPresenter extends BasePresenter<CarConfigSelectInterface> {

    private DataManager dataManager;
    private Subscription mSubscription;
    private CarConfigSelectInterface mView;

    public CarConfigSelectPresenter(DataManager dataManager){
        super();
        this.dataManager = dataManager;
    }
    @Override
    public void attachView(CarConfigSelectInterface mvpView) {
        super.attachView(mvpView);
        mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 请求车系参数
     * @author zealjiang
     * @time 2016/11/2 20:30
     */
    public void requestCarConfig(int ModelId) {
        Map<String,String> params = new HashMap<>();
        params.put("modelId", ModelId+"");
        params.put("userID", AppContext.getContext().getUser().getUserId() + "");
        params.put("tokenID", "6");
        params.put("op", "GetProperty");
        //加sign
        params = MD5Utils.encryptParams(params);


        if (!checkSubmitInfo(params)) return;
        mView.showDialog();
        mSubscription = dataManager.getCarConfig(params).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<CarConfigModel.MemberValueBean>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissDialog();
                        if (e != null && mView != null) {
                            mView.showError(e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(List<CarConfigModel.MemberValueBean> data) {

                        CarConfigModel carConfigModel = new CarConfigModel();
                        carConfigModel.setData(data);
                        mView.carConfigSucceed(carConfigModel);

/*                        int status = data.getStatus();
                        if (status == 100) {
                            mView.carConfigSucceed(data);
                        } else {
                            if(TextUtils.isEmpty(data.getMsg())){
                                return;
                            }
                            mView.showError(data.getMsg());
                        }*/
                    }
                });
    }
}
